package com.yuchen.ishop.web.app.buyer;

import com.yuchen.ishop.entity.*;
import com.yuchen.ishop.service.buyer.BuyerOrderRestClient;
import com.yuchen.ishop.service.goods.GoodsRestClient;
import com.yuchen.ishop.service.seller.SellerRestClient;
import com.yuchen.ishop.service.seller.ShopRestClient;
import com.yuchen.ishop.service.seller.XiaoerRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/my/buyer/order")
public class BuyerOrderAction extends BaseAction {

    @Autowired
    private BuyerOrderRestClient buyerOrderRestClient;

    @Autowired
    private GoodsRestClient goodsRestClient;

    @Autowired
    private SellerRestClient sellerRestClient;

    @Autowired
    private XiaoerRestClient xiaoerRestClient;

    @Autowired
    private ShopRestClient shopRestClient;

    @RequestMapping(value = "index")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        List list = buyerOrderRestClient.getByBuyerId(userInfo.getId());
        if (list != null && list.size() > 0) {
            Map map = null;
            Long goodsId = null;
            Goods goods = null;
            Seller seller = null;
            SellerXiaoer xiaoer = null;
            Shop shop = null;
            for (int i = 0; i < list.size(); i++) {
                map = (Map) list.get(i);
                if (map.get("seller_id") != null) {
                    seller = sellerRestClient.getById(Long.valueOf(map.get("seller_id").toString()));
                    if (seller != null) {
                        seller.setPassword(null);
                        seller.setSalt(null);
                        shop = shopRestClient.findShopBySellerId(seller.getId());
                    }
                }
                map.put("seller", seller);
                map.put("shop", shop);
                if (map.get("xiao_id") != null) {
                    xiaoer = xiaoerRestClient.getXiaoer(Long.valueOf(map.get("xiao_id").toString()));
                    if (xiaoer != null) {
                        xiaoer.setPassword(null);
                        xiaoer.setSalt(null);
                    }
                }
                map.put("xiaoer", xiaoer);
            }
        }
        modelMap.addAttribute("list", list);
        modelMap.addAttribute("myMenu", "myOrder");
        return "my/buyer/myOrder";
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public String insert(HttpServletRequest request, Order order, ModelMap modelMap) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        order.setBuyerId(userInfo.getId());
        order.setCreateTime(getTime());
        buyerOrderRestClient.insert(order);
        return "redirect:/my/buyer/address/index.htm";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(Order order, ModelMap modelMap) {
        buyerOrderRestClient.update(order);
        return "redirect:/my/buyer/address/index.htm";
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(Long id, ModelMap modelMap) {
        buyerOrderRestClient.delete(id);
        return "redirect:/my/buyer/address/index.htm";
    }

    @RequestMapping(value = "get")
    @ResponseBody
    public Object get(Long id, ModelMap modelMap) {
        Order order = buyerOrderRestClient.getById(id);
        return order;
    }
}
